//
//  BaseViewController.m
//  BaseProject
//
//  Created by 喻飞 on 15/5/10.
//  Copyright (c) 2015年 yuf. All rights reserved.
//

#import "BaseViewController.h"

@interface BaseViewController ()<FlipBoardNavigationControllerDelegate>
@property (strong, nonatomic) UITapGestureRecognizer *tapGesture;
@end

@implementation BaseViewController
@synthesize  shareApi;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        //        naviItem = [[CustomNaviItem alloc] init];
        //        [self.navigationItem setHidesBackButton:YES];
        //        naviItem.delegate = self;
        
        _allowSwipeGesture = YES;
        
    }
    return self;
}
- (void)awakeFromNib
{
    [super awakeFromNib];
}

-(void)viewWillAppear:(BOOL)animated{
    
    [super viewWillAppear:YES];
    [self dismissWaitDialog];
   
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
   [self.view setBackgroundColor:[UIColor whiteColor]];
    self.userLatitude = @"";
    self.userLongitude = @"";
    shareApi=[LQRequestApi sharedInstance];
    shareApi.delegate=self;
    self.flipboardNavigationController.delegate = self;
    
    
    [[NSNotificationCenter defaultCenter]  addObserver:self selector:@selector(ProductTypeChoice:) name:Notification_ProductTypeChoice object:nil];
    
    [self.view addGestureRecognizer:self.rightSwipeGesture];
    
    //键盘显示，隐藏
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];

}
- (void)ProductTypeChoice:(NSNotification *)notify{
    
}
//配置导航栏与状态栏
- (void)configurationNavigationAndTabbar{
    //为工具栏设置标题与图片
    UITabBar *tabBar = self.tabBarController.tabBar;
    tabBar.tintColor = [UIColor clearColor];
    tabBar.selectedImageTintColor = [UIColor clearColor];
    tabBar.backgroundColor = [UIColor whiteColor];
    
    UITabBarItem *tabBarItem1 = [tabBar.items objectAtIndex:0];
    UITabBarItem *tabBarItem2 = [tabBar.items objectAtIndex:1];
    UITabBarItem *tabBarItem3 = [tabBar.items objectAtIndex:2];
    UITabBarItem *tabBarItem4 = [tabBar.items objectAtIndex:3];
    UITabBarItem *tabBarItem5 = [tabBar.items objectAtIndex:4];
    
    
    //声明这张图片用原图(别渲染)
    UIColor *selectColor = [UIColor colorWithRed:0.071 green:0.231 blue:0.451 alpha:1.000];
    UIColor *normalColor = [UIColor colorWithWhite:0.196 alpha:1.000];
    UIImage *selectedImage = [UIImage imageNamed:@"nav3_on"];
    selectedImage = [selectedImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
   [tabBarItem3 setFinishedSelectedImage:[[UIImage imageNamed:@"nav3_on"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] withFinishedUnselectedImage:selectedImage];
    [tabBarItem3 setTitleTextAttributes:[NSDictionary dictionaryWithObject:selectColor forKey:NSForegroundColorAttributeName] forState:UIControlStateSelected];
    [tabBarItem3 setTitleTextAttributes:[NSDictionary dictionaryWithObject:normalColor forKey:NSForegroundColorAttributeName] forState:UIControlStateNormal];
    
    
    
    
    UIImage *selectedImage1 = [UIImage imageNamed:@"nav2"];
    selectedImage1 = [selectedImage1 imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    [tabBarItem1 setFinishedSelectedImage:[[UIImage imageNamed:@"nav2_on"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] withFinishedUnselectedImage:selectedImage1];
    [tabBarItem1 setTitleTextAttributes:[NSDictionary dictionaryWithObject:selectColor forKey:NSForegroundColorAttributeName] forState:UIControlStateSelected];
    [tabBarItem1 setTitleTextAttributes:[NSDictionary dictionaryWithObject:normalColor forKey:NSForegroundColorAttributeName] forState:UIControlStateNormal];
    
    UIImage *selectedImage2 = [UIImage imageNamed:@"nav4"];
    selectedImage2= [selectedImage2 imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    [tabBarItem2 setFinishedSelectedImage:[[UIImage imageNamed:@"nav4_on"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] withFinishedUnselectedImage:selectedImage2];
    [tabBarItem2 setTitleTextAttributes:[NSDictionary dictionaryWithObject:selectColor forKey:NSForegroundColorAttributeName] forState:UIControlStateSelected];
    [tabBarItem2 setTitleTextAttributes:[NSDictionary dictionaryWithObject:normalColor forKey:NSForegroundColorAttributeName] forState:UIControlStateNormal];
    
    UIImage *selectedImage4 = [UIImage imageNamed:@"nav1"];
    selectedImage4 = [selectedImage4 imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    [tabBarItem4 setFinishedSelectedImage:[[UIImage imageNamed:@"nav1_on"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] withFinishedUnselectedImage:selectedImage4];
    [tabBarItem4 setTitleTextAttributes:[NSDictionary dictionaryWithObject:selectColor forKey:NSForegroundColorAttributeName] forState:UIControlStateSelected];
    [tabBarItem4 setTitleTextAttributes:[NSDictionary dictionaryWithObject:normalColor forKey:NSForegroundColorAttributeName] forState:UIControlStateNormal];
    
    
    UIImage *selectedImage5 = [UIImage imageNamed:@"nav5"];
    selectedImage5 = [selectedImage5 imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    [tabBarItem5 setFinishedSelectedImage:[[UIImage imageNamed:@"nav5_on"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] withFinishedUnselectedImage:selectedImage5];
    [tabBarItem5 setTitleTextAttributes:[NSDictionary dictionaryWithObject:selectColor forKey:NSForegroundColorAttributeName] forState:UIControlStateSelected];
    [tabBarItem5 setTitleTextAttributes:[NSDictionary dictionaryWithObject:normalColor forKey:NSForegroundColorAttributeName] forState:UIControlStateNormal];


}

- (void)hideKeyBoard{
    [[UIApplication sharedApplication].keyWindow endEditing:YES];
}
#pragma mark  调整自适应布局
- (void) adaptiveViewLayout:(UIView *)viewLayout{
    //视图自适应布局
    for (UIView *view in viewLayout.subviews) {
        view.frame = flexibleFrame(view.frame, NO);
        if ([view isKindOfClass:[UITextField class]]) {
            UITextField *viewField = (UITextField *)view;
            CGFloat fontSize = viewField.font.pointSize;
            viewField.font = [UIFont systemFontOfSize:fontSize*VerticalTexeRatio()];
        }else if ([view isKindOfClass:[UILabel class]]){
            UILabel *viewLabel = (UILabel *)view;
            CGFloat fontSize = viewLabel.font.pointSize;
            viewLabel.font = [UIFont systemFontOfSize:fontSize*VerticalTexeRatio()];
        }else if ([view isKindOfClass:[UIButton class]]){
            UIButton *viewButton = (UIButton *)view;
            CGFloat fontSize = viewButton.titleLabel.font.pointSize;
            viewButton.titleLabel.font = [UIFont systemFontOfSize:fontSize*VerticalTexeRatio()];
        }
        
    }
}
#pragma mark - Custom mehtod
//返回
-(void) back:(id) sender{
    [self.navigationController popViewControllerAnimated:YES];
}
-(void)backBtn{
    [[Common getNavigation]popViewController];
}
-(void)showWaitDialogForNetWork
{
    [self performSelector:@selector(showWaitDialogForWait:) withObject:nil afterDelay:0.001];
}
- (void)showWaitDialog:(NSString*)title state:(NSString*)state{
    
    [CWHUDTips showTips:title];
    
}
- (void)showWaitDialogForWait:(NSString *)str {
    
    [CWHUDTips showLoadingTips:@"加载中..."];
}

-(void)showWaitDialogForNetWorkDismissBySelf
{
    [self showWaitDialogForNetWork];
    double delayInSeconds = 1;
    dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, delayInSeconds * NSEC_PER_SEC);
    dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
        [self dismissWaitDialog];
    });
    
}


-(void)dismissWaitDialog
{
    [CWHUDTips hideTips];
    
}

#pragma mark == 右划手势事件
-(void)action_swipeGesture:(UISwipeGestureRecognizer *)gesture{
    [self backBtn];
}

#pragma mark - 键盘事件=======================================
-(void)Actiondo:(id)sender{
    [[UIApplication sharedApplication].keyWindow endEditing:YES];
    [self.view removeGestureRecognizer:self.tapGesture];
    self.tapGesture = nil;
}

-(void)keyboardWillHide:(NSNotification *)notification
{
    //    NSLog(@"*-----HideKeyBoard");
    [self.view removeGestureRecognizer:self.tapGesture];
    self.tapGesture = nil;
}

-(void)keyboardWillShow:(NSNotification *)notification
{
    //    NSLog(@"*-----ShowKeyBoard");
    if (self.tapGesture) {
        [self.view removeGestureRecognizer:self.tapGesture];
        self.tapGesture = nil;
    }
    self.tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(Actiondo:)];
    [self.view addGestureRecognizer:self.tapGesture];
}



#pragma mark - Initialized

- (void)initMapView
{
    [self clearMapView];
    if (self.mapView == nil)
    {
        self.mapView = [[SharedMapView sharedInstance] mapView];
    }
    
    [[SharedMapView sharedInstance] stashMapViewStatus];
    
    self.mapView.frame = CGRectMake(0, 0, kDeviceWidth, 504*VerticalRatio());
    [self.mapView setZoomLevel:13.5 animated:YES];
    self.mapView.showsCompass= NO;
    self.mapView.showsScale = NO;
    self.mapView.showsUserLocation = YES;
}
#pragma mark - Utility

- (void)clearMapView
{
    if (self.mapView) {
        self.mapView.showsUserLocation = NO;
        
        [self.mapView removeAnnotations:self.mapView.annotations];
        
        [self.mapView removeOverlays:self.mapView.overlays];
        
        self.mapView.delegate = nil;
        
        [[SharedMapView sharedInstance] popMapViewStatus];
        self.mapView = nil;
    }
    if(self.naviManager){
        self.naviManager = nil;
        self.naviManager.delegate = nil;
    }
    if(self.iFlySpeechSynthesizer){
        self.iFlySpeechSynthesizer = nil;
        self.iFlySpeechSynthesizer.delegate = nil;
        
    }
    if(self.naviViewController){
        self.naviViewController = nil;
        self.naviViewController.delegate = nil;
    }
}
#pragma   mark  高德地图导航======================================
#pragma mark - Initialized

- (void)initNaviManager
{
    
    if (self.naviManager == nil)
    {
        self.naviManager = [[AMapNaviManager alloc] init];
    }
    
    self.naviManager.delegate = self;
}
- (void)initIFlySpeech
{
    if (self.iFlySpeechSynthesizer == nil)
    {
        self.iFlySpeechSynthesizer = [IFlySpeechSynthesizer sharedInstance];
    }
    
    self.iFlySpeechSynthesizer.delegate = self;
}
- (void)initNaviViewController
{
    if (self.naviViewController == nil)
    {
        self.naviViewController = [[AMapNaviViewController alloc] initWithMapView:self.mapView delegate:self];
    }
}
- (void)configMapView
{
    [self.mapView setDelegate:self];
    self.mapView.showsUserLocation = YES;
}
- (void)naviManager:(AMapNaviManager *)naviManager didDismissNaviViewController:(UIViewController *)naviViewController
{
    [self configMapView];
}
- (void)naviManagerOnCalculateRouteSuccess:(AMapNaviManager *)naviManager
{
    [self.naviManager presentNaviViewController:self.naviViewController animated:YES];
}
#pragma mark - AManNaviViewController Delegate
- (void)naviManager:(AMapNaviManager *)naviManager didPresentNaviViewController:(UIViewController *)naviViewController
{
    [self.naviManager startGPSNavi];
}
- (void)naviViewControllerCloseButtonClicked:(AMapNaviViewController *)naviViewController
{
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^{
        [self.iFlySpeechSynthesizer stopSpeaking];
    });
    
    [self.naviManager stopNavi];
    [self.naviManager dismissNaviViewControllerAnimated:YES];
}

- (void)naviViewControllerTurnIndicatorViewTapped:(AMapNaviViewController *)naviViewController
{
    [self.naviManager readNaviInfoManual];
}
- (void)naviManager:(AMapNaviManager *)naviManager playNaviSoundString:(NSString *)soundString soundStringType:(AMapNaviSoundType)soundStringType
{
    NSLog(@"playNaviSoundString:{%ld:%@}", (long)soundStringType, soundString);
    
    if (soundStringType == AMapNaviSoundTypePassedReminder)
    {
        //用系统自带的声音做简单例子，播放其他提示音需要另外配置
        AudioServicesPlaySystemSound(1009);
    }
    else
    {
        dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^{
            [self.iFlySpeechSynthesizer startSpeaking:soundString];
        });
    }
}
- (BOOL)naviManagerGetSoundPlayState:(AMapNaviManager *)naviManager
{
    //    NSLog(@"GetSoundPlayState");
    
    return 0;
}

- (UISwipeGestureRecognizer *)rightSwipeGesture
{
    if (!_rightSwipeGesture) {
        _rightSwipeGesture = ({
            UISwipeGestureRecognizer * rightSwipeGesture = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(action_swipeGesture:)];
            rightSwipeGesture.direction = UISwipeGestureRecognizerDirectionRight;
            
            rightSwipeGesture;
        });
    }
    return _rightSwipeGesture;
}


@end
